#!/usr/bin/env python
# -*- encoding: utf-8 -*-
"""
@File    : heapm.py
@Time    : 2023/01/27 12:57:09
@Author  : 郭瑞强
@Contact : sunraing@126.com
@Version : 0.1
@License : BSD 3-Clause License
@Desc    : python标准库的大顶堆只实现了部分功能
"""

from heapq import _siftdown_max, _siftup_max


def heappushm(heap, item):
    """Push item onto heap, maintaining the heap invariant."""
    heap.append(item)
    _siftdown_max(heap, 0, len(heap) - 1)


def heappushpopm(heap, item):
    """Fast version of a heappush followed by a heappop."""
    if heap and heap[0] > item:
        item, heap[0] = heap[0], item
        _siftup_max(heap, 0)
    return item
